package com.grapecity.control.gridview;

import com.grapecity.control.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class CircleGridView extends ViewGroup {

	@SuppressWarnings("unused")
	private ImageAdapter adapter;

	private OnImageClickListener listener;

	private int rows;
	private int columns;
	private int space;
	private int childWidth;
	private int childHeight;

	public interface OnImageClickListener {
		void onImageCilcked(int position, View view);
	}

	public CircleGridView(Context context) {
		this(context, null);
	}

	public CircleGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initView(context, attrs);
	}

	private void initView(Context context, AttributeSet attrs) {
		int defaultSpace = (int) getResources().getDimension(R.dimen.circlegridview_space);
		if (attrs == null) {
			space = defaultSpace;
			return;
		}

		TypedArray a = null;
		try {
			a = context.obtainStyledAttributes(attrs, R.styleable.CircleGridView, 0, 0);
			space = (int) a.getDimension(R.styleable.CircleGridView_space, 0);
		} finally {
			a.recycle();
		}
	}

	private void initMatrix(int length) {
		if (length <= 3) {
			rows = 1;
			columns = length;
		} else if (length <= 6) {
			rows = 2;
			columns = 3;
			if (length == 4) {
				columns = 2;
			}
		} else {
			rows = 3;
			columns = 3;
		}
	}

	public void setAdapter(ImageAdapter adapter) {
		if (adapter == null || adapter.getCount() <= 0) {
			removeAllViews();
			return;
		}

		int oldCount = getChildCount();
		int newCount = adapter.getCount();
		removeScrapViews(oldCount, newCount);
		this.adapter = adapter;
		initMatrix(newCount);
		addChildren(adapter);
	}

	private void removeScrapViews(int oldCount, int newCount) {
		if (newCount < oldCount) {
			removeViews(newCount - 1, oldCount - newCount);
		}
	}

	private void addChildren(ListAdapter adapter) {
		int childCount = getChildCount();
		int count = adapter.getCount();
		for (int i = 0; i < count; i++) {
			boolean hasChild = i < childCount;
			View recycleView = hasChild ? getChildAt(i) : null;
			View child = adapter.getView(i, recycleView, this);

			if (child != recycleView) {
				if (hasChild) {
					removeView(recycleView);
				}

				addView(child);
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int childCount = getChildCount();
		if (childCount <= 0) {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			return;
		}

		final int minW = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
		int width = resolveSizeAndState(minW, widthMeasureSpec, 1);
		int availableWidth = width - getPaddingLeft() - getPaddingRight();
		if (childCount <= 1) {
			childWidth = availableWidth * 2 / 3;
			childHeight = childWidth;
		} else {
			childWidth = (availableWidth - space * (columns - 1)) / 3;
			childHeight = childWidth;
		}
		int height = childHeight * rows + space * (rows - 1);

		setMeasuredDimension(width, height + getPaddingTop() + getPaddingBottom());
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		layoutChildren();
	}

	protected void layoutChildren() {
		if (rows <= 0 || columns <= 0) {
			return;
		}

		final int childCount = getChildCount();
		for (int i = 0; i < childCount; i++) {
			ImageView view = (ImageView) getChildAt(i);

			int row = i / columns;
			int col = i % columns;
			int left = (childWidth + space) * col + getPaddingLeft();
			int top = (childHeight + space) * row + getPaddingTop();
			int right = left + childWidth;
			int bottom = top + childHeight;

			view.layout(left, top, right, bottom);

			final int position = i;
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (listener != null) {
						listener.onImageCilcked(position, v);
					}
				}
			});
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

	@Override
	public void addView(View child) {
		setChildLayoutParams(child);
		super.addView(child);
	}

	private void setChildLayoutParams(View view) {
		ViewGroup.LayoutParams params = view.getLayoutParams();
		if (params != null) {
			params.width = childWidth;
			params.height = childHeight;
		}
	}

	public void setOnImageClickListener(OnImageClickListener listener) {
		this.listener = listener;
	}
}
